package com.googlecode.wmlynar.gwtbeans.gen;

import com.googlecode.wmlynar.gwtbeans.client.beans.Binding;
import com.googlecode.wmlynar.gwtbeans.client.beans.BindingGroup;
import com.googlecode.wmlynar.gwtbeans.client.beans.Property;

/**
 * 
 * @author Wojciech Mlynarczyk (wmlynar@gmail.com)
 * 
 */
public class ReferenceBindingFactory {

    private final static Property p1 = new Property<Bean1, Bean1>() {
        public String getName() {
            return "bean1";
        }

        public Bean1 getValue(final Bean1 o) {
            return o.getBean1();
        }

        public void setValue(final Bean1 o, final Bean1 value) {
            o.setBean1(value);
        }
    };

    private final static Property p2 = new Property<Bean1, String>() {
        public String getName() {
            return "string1";
        }

        public String getValue(final Bean1 o) {
            return o.getString1();
        }

        public void setValue(final Bean1 o, final String value) {
            o.setString1(value);
        }
    };

    private final static Property p3 = new Property<Bean2, String>() {
        public String getName() {
            return "string2";
        }

        public String getValue(final Bean2 o) {
            return o.getString2();
        }

        public void setValue(final Bean2 o, final String value) {
            o.setString2(value);
        }
    };

    public static BindingGroup createBinding(final Bean1 o1, final Bean2 o2,
            final String mappingName) {
        final BindingGroup g = new BindingGroup();

        g.add(new Binding(o1, new Property[] { p1, p2 }, o2, new Property[] { p3 }));

        return g;
    }

    public static void copyAToB(final Bean1 o1, final Bean2 o2, final String mappingName) {
        o2.setString2(o1.getBean1().getString1());
    }

    public static void copyBToA(final Bean1 o1, final Bean2 o2, final String mappingName) {
        o1.getBean1().setString1(o2.getString2());
    }
}
